package com.adgox.tiantianbiting.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.bean.ProgramChooseBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by tang on 2018/12/29.
 */

public class ProgramCustomBuyAdapter extends RecyclerView.Adapter<ProgramCustomBuyAdapter.ViewHolder> implements View.OnClickListener {

    private List<ProgramChooseBean> datas;
    private ArrayList<Integer> selectedPositions;

    public ProgramCustomBuyAdapter(List<ProgramChooseBean> datas, ArrayList<Integer> selectedPositions) {
        this.datas = datas;
        this.selectedPositions = selectedPositions;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_buy_choose, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ProgramChooseBean bean = datas.get(position);
        holder.tvName.setText(bean.getTitle());
        holder.tvNum.setText("第"+bean.getSeason()+"章");
        holder.tvDescribe.setText(bean.getDescribe());
        if (selectedPositions.contains(position)){
            holder.ivSelect.setImageResource(R.drawable.icon_selected);
        } else {
            holder.ivSelect.setImageResource(R.drawable.icon_unselected);
        }

        holder.itemView.setTag(position);
    }


    public void setSelectedPositions(ArrayList<Integer> selectedPositions){
        this.selectedPositions = selectedPositions;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.tv_describe)
        TextView tvDescribe;
        @BindView(R.id.iv_select)
        ImageView ivSelect;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            itemView.setOnClickListener(ProgramCustomBuyAdapter.this);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view,int position);
    }

    public OnItemClickListener listener = null;

    public void onSetItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();
        listener.onItemClick(view,position);
    }
}
